package com.shangma.util;

import com.shangma.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @className: RestTemplateTest
 * @description:
 * @date:2021/12/22 10:23
 */
public class RestTemplateTest {


    public static void main(String[] args) {


    }

    private static void timer() {
        // 创建TimerTask对象
        TimerTask timerTask = new MyWeatherTask();

        Timer timer = new Timer();
        // 延迟5秒执行，每3秒执行一次
        timer.schedule(timerTask,5000,3000);
    }

    private static void allMethodRequest() {
        // get和post请求默认发送的是json数据

        // ctrl + alt + m  抽取方法
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/common/delete/{0}";

        // 需求:请求的时候使用请求头     put和delete需要返回值

        // 创建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token","rgre8g.regregreg8rg4.greg4g54g");
        HttpEntity requestEntity = new HttpEntity(httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(path, HttpMethod.DELETE, requestEntity, String.class, "1,2");
        System.out.println(exchange);
    }

    private static void deleteForData() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/common/delete/{0}";
        restTemplate.delete(path,"1,2,3,4");
    }

    private static void putForData() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/common/update";
        // 可以创建User对象 也可以使用map
        Map<String,Object> map = new HashMap<>();
        map.put("name","王一博");
        map.put("age",18);
        restTemplate.put(path,map);
    }

    private static void postForData() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8080/common/doLogin";
        User user = new User();
        user.setName("王力宏");
        user.setAge(20);
        // 执行post请求  获取响应体信息
        String s = restTemplate.postForObject(path, user, String.class);
        // 执行post请求 获取所有的响应信息
//        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, user, String.class);

        System.out.println(s);
    }

    private static void getForObjectRestful() {
        // 发送get请求  restful风格
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String phone = "18500610302";
        // 远程接口的路径
        String path = "http://localhost:8080/common/findByPhone/{0}";
        // 最后一个参数是可变参数，可以放入多个
        String forObject = restTemplate.getForObject(path, String.class, phone);
        System.out.println(forObject);
    }

    private static void getForEntity() {
        // 发送get请求
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        // 远程接口的路径
        String path = "http://api.tianapi.com/tianqi/index?key=7c4e55e6efd90f0baa513c07bcbffbfb&city=郑州市";
        // 获取的是响应的全部内容
        ResponseEntity<String> forEntity = restTemplate.getForEntity(path, String.class);
        // 获取响应头的信息
        System.out.println(forEntity.getHeaders());
        // 获取响应体的信息
        System.out.println(forEntity.getBody());
        // 获取响应行的信息
        System.out.println(forEntity.getStatusCode());
    }

    private static void getForObject() {
        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        // 远程接口的路径
        String path = "http://api.tianapi.com/tianqi/index?key=7c4e55e6efd90f0baa513c07bcbffbfb&city=郑州市";
        // 获取的是响应体的内容
        String forObject = restTemplate.getForObject(path, String.class);
        System.out.println(forObject);
    }
}
